package com.raysdata.smartcity.flow.activiti.dao;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.engine.*;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 流程DAO
 * Created by liux on 2016/12/08.
 */
@Component
public class IdentityDao{

    Log log = LogFactory.getLog(IdentityDao.class);

    @Autowired
    public IdentityService identityService;
    /**
     * 用户组保存
     * @param id
     * @param name
     * @return
     */
    public int saveUserGroup(String id,String name,String type){
        Group group=identityService.newGroup(id);
        group.setName(name);
        group.setType(type);
        try {
            identityService.saveGroup(group);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 删除用户组
     * @param id
     * @return
     */
    public int deleteUserGroup(String id){
        try {
            identityService.deleteGroup(id);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }
    /**
     * 删除用户组与用户关联信息
     * @param user_id
     * @param group_id
     * @return
     */
    public int deleteMembership(String user_id,String group_id){
        try {
            identityService.deleteMembership(user_id,group_id);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }
    /**
     * 添加用户组与用户关联信息
     * @param user_id
     * @param group_id
     * @return
     */
    public int createMembership(String user_id,String group_id){
        try {
            identityService.createMembership(user_id,group_id);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 用户保存
     * @return
     */
    public int saveUser(String id){
        try {
            User user=identityService.newUser(id);
            identityService.saveUser(user);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }
    /**
     * 用户删除
     * @return
     */
    public int deleteUser(String id){
        try {
            identityService.deleteUser(id);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

}
